﻿using System;
using E_Commerce.Domain.Entities;
using E_Commerce.Domain.Interfaces;//引用
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;//引用AutoMapper
using E_Commerce.Application.Dto;
using E_Commerce.Domain.Dto;

namespace E_Commerce.Application
{
   public class OrderService:IOrderService
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IOrderRepository _orderRepository;
        private IMapper _mapper;
        public OrderService(IOrderRepository orderRepository, IMapper mapper)
        {
            _orderRepository =orderRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 生成预支付订单
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public async Task<int> Apayment(OrdersDto o)
        {
            try
            {
                Orders orders = _mapper.Map<Orders>(o);
                return await _orderRepository.Advancepayment(orders);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleOrderAsync(int id)
        {
            return await _orderRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 反填订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Orders> FillOrderAsync(int id)
        {
            return await _orderRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="OrderNumber"></param>
        /// <param name="totalCountint"></param>
        /// <param name="orderstatic"></param>
        /// <returns></returns>
        public List<OrdersDto> GetOrders(int pageIndex, int pageSize, out int totalCount, string OrderNumber,int orderstatic)
        {
            return _orderRepository.GetOrders(pageIndex, pageSize,out totalCount,OrderNumber, orderstatic);
        }
        /// <summary>
        /// 查看订单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<ProductDto>> OrderFind(int id)
        {
            return await _orderRepository.OrderFind(id);
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public async Task<int> UptOrderAsync(Orders o)
        {
            try
            {
                return await _orderRepository.UpdateAsync(o);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
           
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UptOrderStatic(int id)
        {
            return _orderRepository.UptOrderStatic(id);
        }
    }
}
